package com.pcbly.web.action.system;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.system.MenuChildBean;
import com.pcbly.web.bean.system.ModuleBean;
import com.pcbly.web.business.menu.MenuService;
import com.pcbly.web.business.system.ModuleService;

/**
 * @author carl.song Copyright (c) 2010 Lightsoft, International. All rights
 *         reserved.
 * 
 */

public class ModuleAction extends BaseAction<ModuleBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9135792728178929979L;

	protected ModuleService moduleService;

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	private ModuleBean module;

	private String orderBy;

	private List<ModuleBean> pages;

	private String menuId;

	protected MenuService menuService;

	protected List<MenuChildBean> menuChildBeans;

	private String parentId;

	private boolean asc = true;

	public boolean isAsc() {
		return asc;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public List<MenuChildBean> getMenuChildBeans() {
		return menuChildBeans;
	}

	public void setMenuChildBeans(List<MenuChildBean> menuChildBeans) {
		this.menuChildBeans = menuChildBeans;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public void setAsc(boolean asc) {
		this.asc = asc;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public ModuleBean getModule() {
		return module;
	}

	public List<ModuleBean> getPages() {
		return pages;
	}

	public void setPages(List<ModuleBean> pages) {
		this.pages = pages;
	}

	public void setModule(ModuleBean module) {
		this.module = module;
	}

	public String view() throws Exception {
		long startTime = System.currentTimeMillis();
		module = moduleService.getModuleBeanById(getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.view();
	}

	public String update() throws Exception {
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		module = moduleService.getModuleBeanById(getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.update();
	}

	public String left() throws Exception {
		menuChildBeans = menuService.queryMenuChildBeansByParentId(null);
		return SUCCESS;
	}

	public String child() throws Exception {
		menuChildBeans = menuService.queryMenuChildBeansByParentId(parentId);
		StringBuilder sb = new StringBuilder();
		HttpServletResponse response = getResponse();
		HttpServletRequest request = getRequest();
		String WEBPATH = request.getContextPath();
		sb.append("<?xml  version=\"1.0\" encoding=\"UTF-8\"?><root>");
		for (MenuChildBean bean : menuChildBeans) {
			sb.append("<tree text=\"");
			sb.append(htmEncode(bean.getName()));
			sb.append("\" ");
			if (bean.getChild() < 1) {
				sb.append(" action=\"javascript:showMenu('");
				sb.append(bean.getId());
				// sb.append("','");
				// sb.append(getId());
				sb.append("');\"  icon=\"");
				sb.append(WEBPATH);
				sb.append("/images/xloadtree/page_white_gear.gif\"/>");
			} else {
				sb.append(" action=\"javascript:showMenu('");
				sb.append(bean.getId());
				sb.append("');\"  src=\"");
				sb.append(WEBPATH);
				sb.append("/system/menu/child.crm");
				sb.append("?parentId=");
				sb.append(bean.getId());
				sb.append("\"/>");
			}
		}
		sb.append("</root>");
		response.setContentType("text/xml; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// System.out.println(sb);
		try {
			response.getWriter().write(sb.toString());
			response.getWriter().flush();
			response.getWriter().close();
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String right() throws Exception {
		if (!(menuId == null || "".equals(menuId.trim()))) {

		}
		return SUCCESS;
	}

	public String index() throws Exception {
		// long startTime = System.currentTimeMillis();

		// long start = getPage();
		// start = start * getPageSize();
		//		
		// if (orderBy == null || "".equals(orderBy)) {
		// orderBy = " id ";
		// }
		// pages = new Page<ModuleBean>();
		// pages.setStart(start);
		// pages.setPageSize(getPageSize());
		// pages = moduleService.findModuleBeanByCond(
		// module, pages, orderBy, asc);
		// startTime = System.currentTimeMillis() - startTime;
		// if (log.isDebugEnabled()) {
		// log.debug("page:::::" + getPage());
		// log.debug("pageSize:::::" + getPageSize());
		// log.debug("time ms:::::" + startTime);
		//
		// System.out.println("page:::::" + getPage());
		// System.out.println("pageSize:::::" + getPageSize());
		// System.out.println("time ms:::::" + startTime);
		// }
		return super.index();
	}

	public String add() throws Exception {
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		module = new ModuleBean();
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.add();
	}

	public String save() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(module.getId());
		}
		try {
			if (module.getId() == null || "".equals(module.getId().trim())) {// new
				module.setCreateDate(new Date());
				module.setCreateUserId(getUserId());
				module.setRecycled("0");
				module = moduleService.saveModuleBean(module);
				setMyMessage(getText("entity.saved"));
			} else {// update
				moduleService.updateModuleBean(module);
				setMyMessage(getText("entity.updated"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.save();
	}

	public String dwrSave(ModuleBean bean) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(bean.getId());
		}
		try {
			if (bean.getId() == null || "".equals(bean.getId())) {// new
				moduleService.saveModuleBean(bean);
			} else {// update
				moduleService.updateModuleBean(bean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseSave(bean.getId());
	}

	public String delete() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(getId());
		}
		if (getId().indexOf(",") >= 0) {
			moduleService.removeModuleBeanByIds(getId());
		} else {
			moduleService.removeModuleBeanById(getId());
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		module = null;
		return super.delete();
	}

	public String dwrDelete(String id) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(id);
		}
		if (id.indexOf(",") >= 0) {
			moduleService.removeModuleBeanByIds(id);
		} else {
			moduleService.removeModuleBeanById(id);
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseDelete(id);
	}

}